<?php

class ApiController extends Controller
{
	 
 
	/**
	 * ###########################################################################################
	 * 
	 * 返回初始化的json变量
	 * 
	 */
	public function actionInit()
	{ 
		$ret = array(
			"sendout"=> "6",
			"checkout"=> "3" ,
			"waittime"=> "10",

			"reloginout"=> "500",
			"relogintime"=> "300",
		);
		echo json_encode($ret);
		// set SEND_OUT to sendout of (init_info & {sendout:5})
		// set CHECK_OUT to checkout of (init_info & {checkout:5})
		// set WAIT_TIME to waittime of (init_info & {waittime:20})
		// set RELOGIN_OUT to reloginout of (init_info & {reloginout:30})
		// set RELOGIN_TIME to relogintime of (init_info & {relogintime:10 * 60})

		Yii::app()->end();
	}
	
	/**
	 * 更新一条信息的发送状态
	 * id, 发送的号码
	 * stat，发送状态 0,初始化，1，正在发送，2，成功，3，失败	
	 * task，任务id
	 *  
	 * TODO 需要改成POST，为了以后的加密传输
	 */
	public function actionSendstat()
	{

		echo json_encode(array('code'=> 0, 'msg'=>'提交成功！'));

	}
	/**
	 * 反馈一条扫描信息的状态
	 * id, 发送的号码
	 * stat，发送状态 0,初始化，1，正在扫描，2，成功，3，失败	
	 * task，任务id
	 *  
	 * TODO 需要改成POST，为了以后的加密传输
	 */
	public function actionCheckstat()
	{
		 
		echo json_encode(array('code'=> 0, 'msg'=>'提交成功！'));
			 
	}

	/**
	* 用户登陆
	*
	**/ 
	public function actionRegister(){
		header('Content-type:application/json;charset=utf-8');
		
		$mobile = Yii::app()->request->getParam('mobile'); //$_POST['mobile'];
		$password = Yii::app()->request->getParam('password');


		$model=new User;

		$model->username = $mobile;
		$model->mobile = $mobile;
		$model->email = $mobile.'@nomail.com';
		$model->level = 0;
		$model->credits = 0;

		$model->password = Globals::getPassword($password);



		if($model->save()){

			$ret = array(
				"token" => Globals::getToken($model),
				"mobile"=> $mobile,
				"credits" => 0,
				"level" => 0
			);

			echo json_encode(array( 'code'=> "0", 'msg'=>'注册成功', 'data'=>$ret)); 
		}else{ 
			echo json_encode(array( 'code'=> "1000", 'msg'=>current(current($model->getErrors())), 'data'=>''));
		}
		 
	}
	
	/**
	* 用户登陆
	**/ 
	public function actionLogin(){
		header('Content-type:application/json;charset=utf-8');
		
		$mobile = Yii::app()->request->getParam('mobile'); 
		$password = Yii::app()->request->getParam('password');

		$password = Globals::getPassword($password);

 

		$model= User::model()->find("mobile =:m" , array('m'=> $mobile));

		if($model===null){
			echo json_encode(array( 'code'=> "1000", 'msg'=>'没有找到这个用户', 'data'=>''));
		}else{
			if ($model->password !== $password) {
				echo json_encode(array('code'=> "1000", 'msg'=>'密码错误', 'data'=>''));
			}else{
				 
				$ret = array(
					"token" => Globals::getToken($model),
					"mobile"=> $mobile,
					"credits" => 0,
					"level" => 0
				);

				echo json_encode(array( 'code'=> "0", 'msg'=>'登陆成功', 'data'=>$ret)); 
			}
		}
	}


	/**
	 * 根据机器，获得任务信息
	 * 300 是默认，无任务
	 * 301 没有号码
	 * 302 完成发送任务
	 * @param  [type] $robot [description]
	 * @return [type]        [description]
	 */
	public function actionTask()
	{
		$appleids = array('+8618691915676','+8618691915676','+8618691915676');

		echo json_encode(array('code'=> 0, 'msg'=>'获取任务', 'data'=>array(
				"taskid" => 28,
				"message" => "hello world",
        		"tasktype" => "send",
        		"appleids" => $appleids
			)));

	}



	public function actionTest(){
		 
		// $user = User::model()->find();
		// echo $user->level;
		// echo var_dump(User::$_levels);
		// echo $user->getLevelTitle();

		// Globals::province();
		
		// 统计所有城市数据
		// $citys = Citys::model()->findAll();
		// // echo count($citys);
		// foreach ($citys as $row) {
		// 	$apps = Xapple::model($row->id)->count();

		// 	// echo $apps;
		// 	echo( $row->province ."/".$row->city ."=".$apps.'<br>');

		// 	$row->citytotal = $apps;
		// 	$row->save();
		// }

	
		
		// $ret = array();  
		// foreach($apps as $v)
		// {
		//    $ret[] = array("appleid" => $v['appleid']);
		//    //array_push($ret , array("appleid" => $v['appleid']));
		//    //$v->stat = 1;
		//    //$v->save();
		// }
		 
		// // $robot->stat += count($apps);
		// echo json_encode(array('cmd'=> "check", 'params'=>'', 'dataarea'=>$ret));

		
	}
	
}
